//
//  ViewController.m
//  九宫格
//
//  Created by 互联网 on 16/7/13.
//  Copyright © 2016年 aa. All rights reserved.
//
/*
 1.直接加载所有内容
 2.将部分制作成方法
 3.将字典转模型
 
 */
#import "ViewController.h"
#import "LXAppInfo.h"
#import "Empty.h"


@interface ViewController ()
@property(weak,nonatomic)NSArray *appInfo;
@end

@implementation ViewController
//懒加载
-(NSArray*)appInfo{
    if (_appInfo==nil) {
        
        _appInfo=[LXAppInfo appInfoList];
    
    }
        return _appInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.创建view
    int totalColumn=4;
    CGFloat subviewW=100;
    CGFloat subviewH=100;
   
    for (int i=0; i<[self.appInfo count]; i++) {
        
        Empty *subview=[Empty appInfoViews];
        
        [self.view addSubview:subview];

        //根据plist中存储的文件数量计算子视图的位置和大小
        CGFloat marignX=(self.view.frame.size.width-totalColumn*subviewW)/(totalColumn+1);
        CGFloat marignY=10;
        int comlumn=i%totalColumn;
        int row=i/totalColumn;
        
        CGFloat subviewX = marignX+(subviewW+marignX)*comlumn;
        CGFloat subviewY = 30+(subviewH+marignY)*row;
   
        //subview的位置和大小
        CGRect rect=CGRectMake(subviewX, subviewY, subviewW, subviewH);
       
        subview.frame=rect;
        
        
    /*    //取得数据,方式一
     
        UIImageView *iconView=subview.subviews[0];
        iconView.image=[UIImage imageNamed:appInfo.icon];
        
        //方式2
        UILabel *nameLable=(UILabel *)[subview viewWithTag:2];
        nameLable.text=appInfo.name;
        nameLable.font=[UIFont systemFontOfSize:12];
        
        UIButton *download=(UIButton *)[subview viewWithTag:3];
        [download addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside];
     */
        //使用自定义View
        //为了降低耦合，使用setter方法为其他子view赋值。
//        subview.imageView.image=[UIImage imageNamed:appInfo.icon];
//        subview.nameView.text=appInfo.name;
//        subview.nameView.font=[UIFont systemFontOfSize:12];
        //取出模型对象
        LXAppInfo * appInfo=self.appInfo[i];
        //调用view的setter方法赋值为属性赋值
         subview.appInfo=appInfo;
    }
    }

-(void)downloadClick
{
    
    NSLog(@"下载");
}


//将创建3个控件制作成方法
-(void)viewMakeInfo:(LXAppInfo*) infoDict addView:(UIView*)subView
{
 
    //2.1添加图片视图
    //设置frame
    CGFloat  iconW=60;
    CGFloat  iconH=60;
    CGFloat  iconX=20;
    //创建图片view
    UIImage *image=[UIImage imageNamed:infoDict.icon];
    UIImageView *iconView=[[UIImageView alloc]initWithImage:image];
    CGRect iconRect=CGRectMake(iconX, 0, iconW, iconH);
    [iconView setFrame:iconRect];
    [subView addSubview:iconView];
    //2.2添加标签
    UILabel *nameView=[[UILabel alloc]initWithFrame:CGRectMake(0, 60, subView.frame.size.width, 20)];
    nameView.textAlignment=NSTextAlignmentCenter;
    nameView.text=infoDict.name;
    nameView.font=[UIFont systemFontOfSize:12];
    [subView addSubview:nameView];
    
    //2.3添加按钮
    UIButton *downloadView=[[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(nameView.frame), 60, 20)];
    
    [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen.png"] forState:UIControlStateNormal];
    
    
    [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted.png"] forState:UIControlStateHighlighted];
    [downloadView setTitle:@"下载" forState:UIControlStateNormal];
    [subView addSubview:downloadView];
    //为按钮添加事件
    [downloadView addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside];


}
@end
